package com.idroid.utils;


import android.content.Context;
import android.text.TextUtils;
import android.widget.TextView;

import java.text.DecimalFormat;

public class FormatUtil {
    public static CharSequence getPriceWithRMB(float price) {
        DecimalFormat f = new DecimalFormat("###.00");
        return new StringBuffer("\u00A5 ").append(f.format(price));
    }
    public static CharSequence getPrice(float price) {
        DecimalFormat f = new DecimalFormat("###.00");
        return f.format(price);
    }

    /**
     * 根据string.xml资源格式化字符串
     * @param context
     * @param resource
     * @param args
     * @return
     */
    public static String formatResourceString(Context context, int resource, Object... args) {
        String str = context.getResources().getString(resource);
        if(TextUtils.isEmpty(str)) {
            return null;
        }
        return String.format(str, args);
    }

    /**
     * 银行卡字符每四位分隔，并格式化成16位标准长度
     * @param number
     * @return
     */
    public static void bankcard4Format(TextView tv, String number){
        number = number.substring(number.length()-4);
        number = "************"+number;
        String regex = "(.{4})";
        number = (number.replaceAll (regex, "$1 "));
        tv.setText(number);
    }

    /**
     * 字符每四位分隔
     * @param number
     * @return
     */
    public static void str4Format(TextView tv,String number){
        String regex = "(.{4})";
        number = (number.replaceAll (regex, "$1 "));
        tv.setText(number);
    }
}
